package com.substation.ui.fragment;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.bean.LoginInfo;
import com.interfaces.NovaCallback;
import com.substation.ui.activity.BaseActivity;
import com.substation.ui.activity.LoginActivity;
import com.substation.utils.DialogUtils;
import com.substation.utils.T;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import kr.co.namee.permissiongen.PermissionGen;

/**
 * Created by admin on 2018/5/15.
 */

public abstract class BaseFragment  extends Fragment implements View.OnTouchListener,NovaCallback {

    protected Context context;
    protected View view;
    private boolean isPause;
    Unbinder unbinder;
    protected int errCode;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        view.setOnTouchListener(this);
        return view;
    }


    protected abstract int getLayoutId();

    protected abstract void initView();


    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    public void onResume() {
        super.onResume();
        isPause = false;
    }



    /**
     * 消息提示
     *
     * @param msg
     */
    protected void showMsg(String msg) {
        if (!isPause) {
            ((BaseActivity) getActivity()).showMsg(msg);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }
    @Override
    public void err(int i,int status, String err) {
        if(status==1003){
            LoginInfo.remove(context);
            startActivity(new Intent(context,LoginActivity.class));
            T.showLong(context,err);
            getActivity().finish();
        }else{
            DialogUtils.showMsgDialog(getActivity(),"错误提示",err);
        }
    }

    @Override
    public void success(int i, String obj) {

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(getActivity(), requestCode, permissions, grantResults);
    }
}